<html>
<head>
    <meta charset="UTF-8">
    <title>文件上传</title>
</head>
<link rel="stylesheet" href="/static/layui/css/layui.css"/>
<body>
<form class="layui-form" action="" lay-filter="add">
    <div class="layui-form-item">
        <label class="layui-form-label">患者编号：</label>
        <div class="layui-input-inline layui-col-md4">
            <input type="text" name="patient.puser" id="puser" required lay-verify="required|ausername" placeholder="患者编号"
                   autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">请选择上传文件类别</label>
        <div class="layui-input-block">
            <select name="uposition" lay-filter="aihao">
                <option value="0">遗传诊断报告</option>
                <option value="1">基因测序报告</option>
                <option value="2">诊断病例</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">请选择上传文件</label>
        <input type="file" name="uploadFile" id="uploadFile">
        <input type="text" name="filename" id="uuFile">
        <button type="button" class="layui-btn" id="btn">开始上传</button>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">备注：</label>
        <div class="layui-input-block">
            <textarea name="remark" placeholder="上传文件信息...100个字符以内" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <div class="layui-input-block">
                <button type="submit" class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </div>
</form>
</body>
<script src="/static/js/jquery-3.3.1.js"></script>
<script src="/static/layui/layui.js"></script>
<script>
    //文件上传
    $('#btn').click(function () {
        var formdata = new FormData();
        formdata.append('uploadFile', $('#uploadFile')[0].files[0]);
        $.ajax({
            url: '/uploadFile',
            type: 'post',
            contentType: false,
            processData: false,
            data: formdata,
            success: function (data) {
                console.log(data)
                document.getElementById("uuFile").value = data;
            }
        })
    });
    //Demo
    layui.use(['form', 'upload', 'element', 'laydate'], function () {
        var form = layui.form
            , layer = layui.layer
            , $ = layui.$;
        form.verify({
            ausername: function (value, item) { //value：表单的值、item：表单的DOM对象
                var message = '';
                $.ajax({
                    type: "POST",
                    url: '/chenckPuser',//(对应后台的方法),
                    async: false, //改为同步请求
                    datas: {puser: value},
                    dataType: 'json',
                    success: function (data) {
                        alert(data);
                        if (data.code == 200) {

                        } else {
                            message = "账号不存在，请重新输入！"
                        }
                    }
                });
            }
        });
        //监听提交
        form.on('submit(formDemo)', function (data) {
            var data1 = form.val("add");
            $.ajax({
                url: '/add',
                type: "post",
                dataType: "json",
                data: data1,
                success: function (da) {
                    if (da.code == 200) {
                        layer.alert("添加成功！", function () {
                            // window.parent.location.reload();//刷新父页面
                            // parent.layer.close(layer.index);//关闭弹出层
                            layer.msg("添加成功！");
                        });
                    } else {
                        layer.msg("添加失败！");
                    }
                }
            });
            return false;
        });
    });
</script>
</html>